
/**
 *
 *
 * @export
 * @param {any} obj
 */
import EXIF from './exif-small.js'
export default function ImgCompress (obj) {
  console.log(obj.file)
  this.file = obj.file
  this.limitWidth = obj.limitWidth
  this.limitSize = obj.limitSize || 1024 * 1024
  // this.quality = obj.quality || 0.7
  this.callback = obj.callback
  this.locFilePath = null
  this.run()
}

ImgCompress.prototype = {
  run: function () {
    // console.log('>>>run')
    let _this = this
    let fileReader = new FileReader()
    fileReader.readAsDataURL(this.file)
    fileReader.onload = function () {
      _this.locFilePath = this.result
      if (_this.file.size > _this.limitSize) {
        _this.canvasDataURL()
      } else {
        if (_this.callback) _this.callback(_this.locFilePath, _this.file)
      }
    }
  },
  canvasDataURL: function () {
    // console.log('>>>canvasDataURL')
    let _this = this
    let img = new Image()
    img.src = this.locFilePath
    img.onload = function () {
      // 默认按比例压缩
      let width = this.width
      let height = this.height
      let scale = width / height
      width = _this.limitWidth > width ? width : _this.limitWidth
      height = Math.floor(width / scale)
      let quality = _this.limitSize / _this.file.size
      quality = quality > 0.5 ? quality : 0.5
      // 生成canvas
      let canvas = document.createElement('canvas')
      canvas.width = width
      canvas.height = height
      let ctx = canvas.getContext('2d')
      // 图片方位纠正
      EXIF.getData(_this.file, function () {
        let orientation = EXIF.getTag(this, 'Orientation')
        console.log(orientation)
        if (!orientation) orientation = 1
        switch (orientation) {
          case 1:
            canvas.width = width
            canvas.height = height
            ctx.drawImage(img, 0, 0, width, height)
            break
          case 6:
            canvas.width = height
            canvas.height = width
            ctx.rotate(90 * Math.PI / 180)
            ctx.drawImage(img, 0, -height, width, height)
            break
          case 8:
            canvas.width = height
            canvas.height = width
            ctx.rotate(-90 * Math.PI / 180)
            ctx.drawImage(img, -width, 0, width, height)
            break
          case 3:
            canvas.width = width
            canvas.height = height
            ctx.rotate(180 * Math.PI / 180)
            ctx.drawImage(image, -width, -height, width, height)
            break
        }
        // quality值越小，所绘制出的图像越模糊
        let base64 = canvas.toDataURL('image/jpeg', quality)
        let file = _this.dataUrlToFile(base64, _this.file.name)
        console.log({
          quality,
          orientation,
          sizeChange: `${_this.file.size}>>>${file.size}`
        })
        if (_this.callback) _this.callback(base64, file)
      })
    }
  },
  dataUrlToFile: function (dataurl, filename) {
    let _this = this
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new File([u8arr], filename, { type: mime })
  }
}
